<?php

class Popularity {
	
	function Popularity() {
	}
	
	//Google results
	function get_google($url) {
		$url = 'http://www.google.com/search?q=link:'.$url;
		$contents = file_get_contents($url);
		$results = 0;
		
		if ($contents !== false) {
			$regexp = "/Results\s+([0-9]+)\s+\-\s+([0-9]+)\s+of\s+about\s+([0-9]+)\s+linking\s+to/i";
			$contents = strip_tags($contents);
			
			if (preg_match($regexp, $contents, $matches) ) {
				//print_r($matches);
				if (isset($matches[3])) {
					$results = $matches[3];
				}
			} 
		
		}
		return $results;
	}
	
	//Yahoo Results
	function get_yahoo($url) {
		$url = 'http://siteexplorer.search.yahoo.com/search?p=http://'.$url.'&bwm=i&bwms=p&bwmf=u&fr=sfp&fr2=seo-rd-se';
		//$url = urlencode($url);
		$contents = file_get_contents($url);
		$results = 0;
		
		if ($contents !== false) {
			//$regexp = "/Inlinks\s+([0-9]+)\s+\-\s+([0-9]+)\s*of\s+about\s+([0-9]+)/i";
			$regexp = "/Inlinks\s*\(([0-9]+)\)/i";
			
			//$contents = strip_tags($contents);
			
			if (preg_match($regexp, $contents, $matches) ) {
				//print_r($matches);
				if (isset($matches[1])) {
					$results = $matches[1];
				}
			} 
		
		}
		return $results;
	}
	
	// MSN Results
	function get_msn($url) {
		$url = 'http://search.msn.com/results.aspx?q=link%3A'.$url;
		$contents = file_get_contents($url);
		$results = 0;
		
		if ($contents !== false) {
			$regexp = "/Page\s+([0-9]+)\s+\of\s+([0-9]+)\s+results/i";
			$contents = strip_tags($contents);
			
			if (preg_match($regexp, $contents, $matches) ) {
				//print_r($matches);
				if (isset($matches[2])) {
					$results = $matches[2];
				}
			} 
		
		}
		return $results;
	}
}

$p = new Popularity();
echo $p->get_google('www.googleguide.com');echo "<br>";
echo $p->get_yahoo('www.cybage.com');echo "<br>";
echo $p->get_msn('www.cybage.com');
?>
